package com.tool4j.controller;

import com.tool4j.common.exception.BizException;
import com.tool4j.util.FileReaderUtil;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.http.codec.multipart.Part;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/file")
public class FileController {

    @PostMapping(value = "/readFile", consumes = "multipart/form-data")
    public Mono<String> upload(@RequestPart("file") Part file) {
        try {
            if (file instanceof FilePart filePart) {
                return FileReaderUtil.getText(filePart);
            }
        } catch (Exception e) {
            throw new BizException("文件解析失败");
        }
        return null;
    }

}
